package com.antdesignbackend.backend.city.controller;

import java.io.Serializable;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.city.entity.City;
import com.antdesignbackend.backend.city.entity.entityVo.CityVo;
import com.antdesignbackend.backend.city.service.CityService;

/**
 * 地市管理
 *
 * @author 不二
 * @email jack.com
 * @date 2020-07-21 17:22:28
 */

@Controller
@RequestMapping("/city")
@Slf4j
public class CityController extends BaseController{

	private static final String FUN_NAME = "地市管理";//功能名称

	@Autowired
	private CityService cityService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "city/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody CityVo cityVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<City> wrapper = new QueryWrapper<>();
		if (cityVo.getCity_name() != null && !cityVo.getCity_name().equals("")){
			wrapper.like("city_name",cityVo.getCity_name());
		}
		Page<City> page = new Page<>(cityVo.getPageIndex(), cityVo.getPageSize());
		IPage<City> pageResult = cityService.page(page, wrapper);
		result.setTotal((int)pageResult.getTotal());
		result.setData(pageResult.getRecords());
		return result;

	}

	@GetMapping("/getById/{city_id}")
	@ResponseBody
	public	City getById(@PathVariable(name = "city_id") String city_id) {
		City obj = cityService.getById(city_id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody City city){
		ReturnVO result = new ReturnVO();
		try {
			if(cityService.save(city)){
				result.setMessage("保存成功！");
				result.setCode("200");
			}else{
				result.setMessage("保存失败！");
				result.setCode("500");
			}
		}catch (Exception e){
			log.error(e.getMessage());
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody City city){
		ReturnVO result = new ReturnVO();
		if(cityService.updateById(city)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (cityService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

	/**
	 * 查询所有城市
	 * @return
	 */
	@GetMapping("/getAllCity")
	@ResponseBody
	public List<City> getAllCity() {
		return cityService.list();
	}
}
